package org.example.douban.spider;

import org.apache.log4j.Logger;
import org.example.douban.dao.BookInfoRepository;
import org.example.douban.dao.BookUrlsRepository;
import org.example.douban.parser.BookParser;
import org.example.framework.domain.douban.BookInfo;
import org.example.framework.domain.douban.BookUrls;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

public class BookInfoSpider implements Runnable {
	public static ApplicationContext applicationContext;
	private ConcurrentLinkedQueue<String> inputQueue = new ConcurrentLinkedQueue<String>();
	// book info
	private BookParser bookParser;
	// book url list
	private List<String> urlsList;
	// book tag
	private String tag;

	private Logger log = Logger.getLogger(BookUrlsSpider.class.getName());

	public BookInfoSpider(String tag, BookParser bookParser) {
		this.tag = tag;
		this.bookParser = bookParser;
	}

	public BookInfoSpider(List<String> tagsList, BookParser bookParser) {
		for (String tag : tagsList) {
			inputQueue.offer(tag);
		}
		this.bookParser = bookParser;
	}
	@Override
	public void run() {
		//查询出该标签的url
		BookUrlsRepository bookUrlsRepository = applicationContext.getBean(BookUrlsRepository.class);
		BookInfoRepository bookInfoRepository = applicationContext.getBean(BookInfoRepository.class);
		List<BookUrls> byTag = bookUrlsRepository.findByTag(tag);
		urlsList = new ArrayList<>();
		for (BookUrls bookUrls : byTag) {
			urlsList.add(bookUrls.getBookUrl());
		}
		log.info("[INFO] start running..." + tag);
		try {
			while (!urlsList.isEmpty()) {
				//获取左边队列url,并删除
				String bookUrl = getUrl();
				BookUrls firstByBookUrl = bookUrlsRepository.findFirstByBookUrlAndTag(bookUrl,tag);
				if(firstByBookUrl == null || firstByBookUrl.getIsCrawled() == 1  ){
					continue;
				}
				BookInfo bookInfo = new BookInfo();
				bookParser.bookParser(bookUrl,tag, bookInfo);
					//保存信息
				System.out.printf("执行保存操作");
				bookInfoRepository.save(bookInfo);


				System.out.println(bookInfo);
				//Thread.sleep(900);
				bookUrlsRepository.updateIsCrawledBybookurl(1,bookUrl,tag);
			}
			//更新

			//Thread.sleep(2000);
		} catch (Exception e) {
			log.error("[ERROR] database connected error:", e);
		}

	}

	public String getUrl() {
		return urlsList.remove(0);
	}

}
